package telepasea.app;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class ProfileUI extends Activity {
	/** Called when the activity is first created. */
	private static String USER_NAME = "";
	private static String USER_SEX = "";
	private static String USER_BIRTH = "";
	
	private static final String S_APP = "app";
	private static final String S_SENDER = "sender";
	private static final String S_REGISTRATION_ID_KEY = "S_REGISTRATION_ID";
	private static final String S_AUTH_TOKEN_KEY = "S_AUTH_TOKEN";
	private static String S_REGISTRATION_ID = "";
	private static String S_AUTH_TOKEN = "";
	public static UserProfile myProfile;

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
			setContentView(R.layout.profile);

		final EditText eb = (EditText) findViewById(R.id.username);
		final RadioGroup sexGroup = (RadioGroup) findViewById(R.id.radioGroup1);
		RadioButton mrb = (RadioButton) findViewById(R.id.radio0);
		RadioButton frb = (RadioButton) findViewById(R.id.radio1);

		final SharedPreferences prefs = getSharedPreferences("telepasea",
				MODE_PRIVATE);
		final SharedPreferences.Editor ed = prefs.edit();
		USER_SEX = prefs.getString("user_sex", "");
		USER_BIRTH = prefs.getString("user_birth", "");
		USER_NAME = prefs.getString("user_name", "");

		final Calendar calDateTime = Calendar.getInstance();

		if (!USER_SEX.equals("")) {
			if (USER_SEX.equals("1"))
				mrb.setChecked(true);
			else if (USER_SEX.equals("2"))
				frb.setChecked(true);
			else
				Log.v("pref error", "not male or female");
		}
		if (!USER_BIRTH.equals("")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = sdf.parse(USER_BIRTH);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			calDateTime.setTime(date);

		}
		if (!USER_NAME.equals("")) {
			eb.setText(USER_NAME);

		}

		final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
		dp.init(calDateTime.get(Calendar.YEAR),
				calDateTime.get(Calendar.MONTH),
				calDateTime.get(Calendar.DATE), new OnDateChangedListener() {

					public void onDateChanged(final DatePicker view,
							final int year, final int monthOfYear,
							final int dayOfMonth) {

						calDateTime.set(Calendar.YEAR, year);
						calDateTime.set(Calendar.MONTH, monthOfYear);
						calDateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

					}
				});

		Button pbut = (Button) findViewById(R.id.pbutton);

		pbut.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {

				// int mPos = s2.getSelectedItemPosition();
				// String pvalue = (String)s2.getItemAtPosition(mPos);
				String name = eb.getText().toString();
				String sex;
				if (sexGroup.getCheckedRadioButtonId() == R.id.radio0) {
					ed.putString("user_sex", "1");
					sex = "1";
				} else {
					ed.putString("user_sex", "2");
					sex = "2";

				}
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

				ed.putString("user_name", name);
				ed.putString("user_birth",
						formatter.format(calDateTime.getTime()));

				UserProfile profile = new UserProfile();
				profile.setBirth(calDateTime);
				profile.setName(name);
				profile.setGender(Integer.parseInt(sex));
				
				TelepaSEAActivity.cm.setProfile(profile);
				// cm.addActivity(regActivity, loc);

				ed.commit();
				finish();
			}

		});

	}
}